package com.deelete.springcloud.controller;

import com.deelete.springcloud.pojo.Account;
import com.deelete.springcloud.service.AccountService;
import com.deelete.springcloud.utils.CommonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;

@RestController
@Slf4j
public class AccountController {

    @Resource
    private AccountService accountService;

    /**
     * 操作账户
     * @param userId 用户id
     * @param money  金额
     * @return
     */
    @RequestMapping("/account/cutAccount")
    public CommonResult<Account> CutAccount(@RequestParam("userId") Long userId, @RequestParam("money") BigDecimal money){
        CommonResult<Account> result = new CommonResult<>();
        if (userId == 0L || money.toString().equals('0')){
            result.setStatus(CommonResult.STATUS_ERROR);
            result.setMessage("请求信息不能为空: "+userId+" "+money.toString());
        }else {
            //开始判断操作数据库是否成功
            if(accountService.CutAccount(userId, money)){
                result.setStatus(CommonResult.STATUS_SUCCESS);
                result.setMessage("操作数据库成功");
            }else {
                result.setStatus(CommonResult.STATUS_ERROR);
                result.setMessage("操作数据库失败: "+userId+" "+money.toString());
            }
        }

        return result;

    }


}
